package com.shtel.paas.monitorcenter.config.management.alarm.config.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmConfigDto;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmConfigEntity;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmConfigParam;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesDto;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesEntity;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesParam;
import com.shtel.paas.monitorcenter.config.management.alarm.config.mapper.warningConfigDao;
import com.shtel.paas.monitorcenter.config.management.alarm.config.service.IWarningConfigService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class WarningConfigService implements IWarningConfigService {

	@Autowired
	warningConfigDao dao;
	
	@Override
	public PageInfo<AlarmConfigDto> selectAll(String monitoringIndex,Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<String> indexList = dao.selectMonitorIndex(monitoringIndex);
		PageInfo<AlarmConfigDto> pageInfo = new PageInfo(indexList);
		Long total = pageInfo.getTotal();
		int pages = pageInfo.getPages();
		List<AlarmConfigDto> dtoList = new ArrayList<AlarmConfigDto>();

		List<AlarmConfigEntity> resultList = dao.selectAll(monitoringIndex);
		
		for(String index : indexList){
			AlarmConfigDto dto = new AlarmConfigDto();
			for(AlarmConfigEntity entity : resultList){
			
				if(entity.getMonitorIndex().equals(index)){
					dto.setDesc(entity.getMonitorIndex());
					dto.setName(entity.getKpi());
					if(entity.getLevels().equals("1")){
						dto.setLv1min(entity.getMin());
						dto.setLv1max(entity.getMax());
					}else if(entity.getLevels().equals("2")){
					dto.setLv2min(entity.getMin());
					dto.setLv2max(entity.getMax());
				}else if(entity.getLevels().equals("3")){
				dto.setLv3min(entity.getMin());
				dto.setLv3max(entity.getMax());
			}
			
		}
			}
			dtoList.add(dto);
			}
		//pageInfo.setList(dtoList);
		PageInfo<AlarmConfigDto> repageInfo = new PageInfo(dtoList);
		repageInfo.setTotal(indexList.size());
		repageInfo.setPages(pages);
		return repageInfo;
	}

	@Override
	public List<String> selectMonitorIndex() {
		
		return dao.selectMonitorIndex(null);
	}

	@Override
	public int updateConfig(AlarmConfigParam alarmConfigParam) {
		int i =0;
		if(alarmConfigParam.getWarningLv1ValueHigh()!="" || alarmConfigParam.getWarningLv1ValueLow() !=""){
			AlarmRulesEntity param = new AlarmRulesEntity();
			param.setDesc(alarmConfigParam.getDesc());
			param.setLevel("1");
			param.setMax(alarmConfigParam.getWarningLv1ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv1ValueLow());
			i=dao.updateConfig(param);
		}
		if(alarmConfigParam.getWarningLv2ValueHigh()!="" || alarmConfigParam.getWarningLv2ValueLow() !=""){
			AlarmRulesEntity param = new AlarmRulesEntity();
			param.setDesc(alarmConfigParam.getDesc());
			param.setLevel("2");
			param.setMax(alarmConfigParam.getWarningLv2ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv2ValueLow());
			i=dao.updateConfig(param);
		}
		if(alarmConfigParam.getWarningLv3ValueHigh()!="" || alarmConfigParam.getWarningLv3ValueLow() !=""){
			AlarmRulesEntity param = new AlarmRulesEntity();
			param.setDesc(alarmConfigParam.getDesc());
			param.setLevel("3");
			param.setMax(alarmConfigParam.getWarningLv3ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv3ValueLow());
			i=dao.updateConfig(param);
		}
		return i;
}

	@Override
	public int insertConfig(AlarmRulesDto alarmConfigParam) {
		int i =0;
		if(alarmConfigParam.getWarningLv1ValueHigh()!="" || alarmConfigParam.getWarningLv1ValueLow() !=""){
			AlarmRulesParam param = new AlarmRulesParam();
			param.setDesc(alarmConfigParam.getDesc());
			param.setKpi(alarmConfigParam.getKpi());
			param.setMax(alarmConfigParam.getWarningLv1ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv1ValueLow());
			param.setLevel("1");
			i=dao.insertConfig(param);
		}
		if(alarmConfigParam.getWarningLv2ValueHigh()!="" || alarmConfigParam.getWarningLv2ValueLow() !=""){
			AlarmRulesParam param = new AlarmRulesParam();
			param.setDesc(alarmConfigParam.getDesc());
			param.setKpi(alarmConfigParam.getKpi());
			param.setMax(alarmConfigParam.getWarningLv2ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv2ValueLow());
			param.setLevel("2");
			i=dao.insertConfig(param);
		}
		if(alarmConfigParam.getWarningLv3ValueHigh()!="" || alarmConfigParam.getWarningLv3ValueLow() !=""){
			AlarmRulesParam param = new AlarmRulesParam();
			param.setDesc(alarmConfigParam.getDesc());
			param.setKpi(alarmConfigParam.getKpi());
			param.setMax(alarmConfigParam.getWarningLv3ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv3ValueLow());
			param.setLevel("3");
			i=dao.insertConfig(param);
		}
		return i;
	}

	@Override
	public int deleteConfig(String desc) {
		
		return dao.deleteConfig(desc);
	}
}

